// 引用用户模版数据
const User = require('../models/user.js');
const userController = {
  // showUser 获取用户数据并返回到页面
  showUser: async function (req, res, next) {
    try {
      let userData = await User.all()
      res.json({
        code: 200,
        message: "操作成功",
        data: userData
      })
    } catch (e) {
      console.log(e)
      res.json({ code: 0, message: "操作失败", data: e })
    }
  },
  addUser: async function (req, res, next) {
    if (req.body.name && req.body.number) {
      userData = {
        "name": req.body.name,
        "number": req.body.number
      }
      try {
        let userinfo = await User.insert(userData)
        res.json({
          code: 200,
          message: "操作成功",
          data: userinfo
        })
      } catch (e) {
        res.json({ code: 0, message: '新增失败', data: e })
      }
    } else {
      res.json({ code: -1, message: '格式错误', data: e })
    }


  },
  deleteUser: async function (req, res, next) {
    if (req.body.id) {
      try {
        let userinfo = await User.delete(req.body.id)
        res.json({
          code: 200,
          message: "删除成功",
          data: userinfo
        })
      } catch (e) {
        res.json({ code: 0, message: '删除失败', data: e })
      }
    } else {
      res.json({ code: -1, message: '未找到此id', data: '请重新输入' })
    }


  }
}

module.exports = userController;